1 #include "SMovieTheater.h"
2
3 const
int g_kArraySize = 8; // number of movies that will be available at our theater
4
5 int
main ()
6 {
7     Movie movie_objects[g_kArraySize];
8
9     MovieTicketMaster * p_MovieTicketMaster =
new MovieTicketMaster ("AMC25","SAN JOSE, CA");
10     p_MovieTicketMaster->Init(movie_objects, g_kArraySize);
11     p_MovieTicketMaster->Run();
12
13     delete p_MovieTicketMaster;
14     
return 0;
15 }

16
17 //
default constructor setting pointer to NULL
18
19 MovieTicketMaster::MovieTicketMaster()
20 {
21     p_MovieList =NULL;
22 }

23
24 //non-
default constructor getting parameters for
25 //theater name and location and still initializing the pointer

26
27 MovieTicketMaster::MovieTicketMaster(
string my_theater_name, string my_theater_location)
28 {
29     theater_name = my_theater_name;
30     theater_location = my_theater_location;
31     p_MovieList = NULL;
32 }

33
34 //
public destructor
35 MovieTicketMaster::~MovieTicketMaster()
36 {
37     cout <<
"Theater " << theater_name << ",Location " << theater_location
38         << 
" has been closed.." << endl;
39 }

40
41 // Init
is filling the array of movie objects
42
43 void
MovieTicketMaster::Init(Movie * p_my_MovieList, int array_size)
44 {
45     
string movie_titles[g_kArraySize] = {"Avengers Infinity War",
46         
"Black Panther",
47         
"Venom",
48         
"Antman and the wasp",
49         
"Tomb Raider",
50         
"Fantastic Beasts",
51         
"The Nun",
52         
"The Divergent Series: Allegiant"};
53
54     
int theater_tickets[g_kArraySize] = {100,75,90,90,75,115,120,130};
55
56     
double movie_price[g_kArraySize] = {11.5,11.5,11.5,11.5,11.5,13.5,13.5,15.0};
57
58     Movie * p = p_my_MovieList;
59
60     
for(int i=0;i <= g_kArraySize - 1;i++){
61         p->set_movie_name(movie_titles[i]);
62         p->set_tickets_available(theater_tickets[i]);
63         p->set_ticket_price(movie_price[i]);
64         p++;
65     }
66
67     p_MovieList = p_my_MovieList;
68
69 }

70
71 //
this member is running a do while loop to ask for user selection
72
73 void
MovieTicketMaster::Run()
74 {
75     
int option;
76
77     
do{
78
79         DisplayMenu();
80
81         cout << endl <<
"\t\t\t\tPlease select an option: ";
82
83         cin >> option;
84
85         
switch (option)
86         {
87
88         
case 1:
89             ViewMovies();
90             system(
"PAUSE");
91             system(
"CLS");
92             
break;
93
94         
case 2:
95
96             SearchMovie();
97             system(
"PAUSE");
98             system(
"CLS");
99             
break;
100
101         
case 3:
102
103             PurchaseTicket();
104             
break;
105
106         
case 4:
107             
108             cout <<
"\nBrought To You By code-projects.org\n" << endl;
109             
break;
110             
111         
default:
112
113             cout <<
"Please choose a valid option:" << endl
114                 << 
"=============================" << endl;
115             cin.ignore();
116
117         }
118     }
119     
while (option != 4);
120 }

121
122 //
this member has menu with banner, stating the name and location while showing options
123
124 void
MovieTicketMaster::DisplayMenu()
125 {
126
127         cout<<
"\t\t\t\tTheater: " << theater_name << " at " << theater_location << endl
128         << endl <<
"\t\t\t\t *********************** " << endl
129         <<
"\t\t\t\t|| WELCOME TO ||" << endl
130         <<
"\t\t\t\t|| AMC 25 ||" << endl
131         <<
"\t\t\t\t *********************** " << endl << endl
132         <<
"\t\t\t\tSimple Movie Theater System" << endl
133         <<
"\t\t\t\t===========================" << endl
134         <<
"\t\t\t\t\tMAIN MENU: " << endl
135         <<
"\t\t\t\t1--> View all movies" << endl
136         <<
"\t\t\t\t2--> Search a movie" << endl
137         <<
"\t\t\t\t3--> Purchase tickets" << endl
138         <<
"\t\t\t\t4--> Quit" << endl;
139     
140 }

141
142 //
this member uses a for loop to show movie objects (title,tickets available, price)
143
144 void
MovieTicketMaster::ViewMovies()
145 {
146
147     Movie * p_view_movies = p_MovieList;
148     system(
"CLS");
149     cout << endl
150         <<
"Here are the movies that are showing " << endl
151         <<
"at AMC 25:" << endl
152         <<
"=========================" << endl << endl;
153
154     
for (int i = 0; i < g_kArraySize; i++){
155         cout <<
"Movie showing at theater " << i + 1 << ": " << endl;
156         p_view_movies->Display();
157         ++p_view_movies;
158     }
159 }

160
161 //
this member asks the user to enter a movie name, then uses FindMovie to
162 //
get a pointer, then displays the object //c-p
163
164 Movie * MovieTicketMaster::SearchMovie()
165 {
166     
string search_movie_name;
167     
168     cout <<
"\n\t\t\t\tPlease enter a movie name: ";
169     cin.sync();
170     std::getline(std::cin, search_movie_name);
171
172     Movie * p = FindMovie(search_movie_name);
173
174     
if (p != NULL){
175         p->Display();
176     }
177     
else {
178         cout << endl <<
"\n\t\t\t\tError: Movie not found." << endl << endl;
179         
return NULL;
180     }
181     
return p;
182 }

183
184 //
this member uses a for loop to search through the objects' names
185 // then uses an
if loop to match them up and return a pointer back to the object
186
187 Movie * MovieTicketMaster::FindMovie(
string find_movie_name)
188 {
189
190     Movie * p_find_movie = p_MovieList;
191     
//func1353c
192     
for (int i = 0; i < g_kArraySize; i++){
193         
if (p_find_movie->get_movie_name() == find_movie_name){
194             cout << endl <<
"\n\t\t\t\tMovie found" << endl;
195             
return p_find_movie;
196         }
197         
else {
198             p_find_movie++;
199         }
200     }
201     
return NULL;
202 }

203
204 //
this member allows the user to purchase a certain amount of tickets
205 //
by using the returned pointer from the FindMovie member
206
207 void
MovieTicketMaster::PurchaseTicket()
208 {
209     
string purchase_movie_name;
210
211     
int total_tickets;
212     system(
"CLS");
213     cout <<
"\n\n\t\t\t\tPlease enter a movie name: ";
214     cin.sync();
215     std::getline(std::cin, purchase_movie_name);
216
217     cout <<
"\n\t\t\t\tPlease enter the total amount of tickets you wish to purchase: ";
218     cin >> total_tickets;
219
220     Movie * p = FindMovie(purchase_movie_name);
221
222         
if (p != NULL){
223         
double total_cost = p-> PurchaseTicket(total_tickets);
224                 
if ( total_cost == -1){
225                     cout <<
"\n\t\t\t\tThis movie has been sold out or there are" << endl
226                         << 
"\n\t\t\t\tnot enough tickets available per your request" << endl << endl;
227                 }
228                 
else {
229                     cout <<
"\n\t\t\t\tYour total comes out to: " << total_cost << endl;
230                 }
231                 cout<<
"\n\n\n\n\t\t\t\t";
232                 system(
"PAUSE");
233                 system(
"CLS");
234         }
235         
else {
236             cout <<
"No movie has been found" << endl;
237         }
238         
239 }
240
241
242 Movie::Movie()
243 {
244     movie_name =
"";
245     tickets_available =
0;
246     ticket_price =
0.0;
247 }
248
249
250 Movie::Movie(
string my_movie_name, int my_tickets_available, double my_ticket_price)
251 {
252     movie_name = my_movie_name;
253     tickets_available = my_tickets_available;
254     ticket_price = my_ticket_price;
255 }
256
257
258 Movie::~Movie()
259 {
260     cout << movie_name <<
" is no longer showing.." << endl;
261 }

262
263
264 string
Movie::get_movie_name()
265 {
266     
return movie_name;
267 }

268
269
270 int
Movie::get_tickets_available()
271 {
272     
return tickets_available;
273 }

274
275
276 double
Movie::get_ticket_price()
277 {
278     
return ticket_price;
279 }

280
281
282 void
Movie::set_movie_name(string new_movie_name)
283 {
284     movie_name = new_movie_name;
285 }

286
287
288 void
Movie::set_tickets_available(int new_tickets_available)
289 {
290     tickets_available = new_tickets_available;
291 }

292
293
294 void
Movie::set_ticket_price(double new_ticket_price)
295 {
296     ticket_price = new_ticket_price;
297 }

298
299 //
this member uses the users wanted tickets to calculate their total
300
301 double
Movie::PurchaseTicket(int purchase_tickets_available)
302 {
303     
double total_price;
304
305     
if (purchase_tickets_available <= tickets_available) {
306         total_price = purchase_tickets_available * ticket_price;
307         tickets_available = tickets_available - purchase_tickets_available;
308         
return total_price;
309     }
else {
310         
return -1;
311     }
312 }

313
314 //
this member displays all three aspects of the movie object
315
316 void
Movie::Display()
317 {
318         cout.setf(ios::
fixed, ios::floatfield);
319         cout.setf(ios::showpoint);
320         cout.precision(
2);
321         cout << endl <<
"\t\t\t\tMovie name: " << movie_name << endl
322             <<
"\n\t\t\t\tTickets available: " << tickets_available << endl
323             <<
"\n\t\t\t\tPrice per ticket: " << ticket_price << endl << endl;
324             
325 }


Gõ tìm kiếm nhanh...